home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-24 | 15.0 KB | 500 lines | [TEXT/MPS ] |
- // Copyright © 1994-95 by Apple Computer, Inc. All rights reserved.
- // ToolsMenu.cp
-
- #ifndef __TOOLSMENU__
- #include "ToolsMenu.h"
- #endif
-
- // MacApp
-
- #ifndef __UWINDOW__
- #include <UWindow.h>
- #endif
-
- #ifndef __ULIST__
- #include "UList.h"
- #endif
-
- #ifndef __UMACAPPUTILITIES__
- #include <UMacAppUtilities.h>
- #endif
-
- // DrawShapes
-
- #ifndef __USHAPES__
- #include "UShapes.h"
- #endif
-
- //----------------------------------------------------------------------------------------
- // Globals
-
- TToolsMenu* gToolsMenu;
- TWindow* gToolsPaletteWindow;
-
- //TShape* gShapesArray[kShapesInPalette]; // prototype shapes for the palette
-
- TList* gProtoShapes = NULL;
-
- #define cChangeTool 1014
-
- //----------------------------------------------------------------------------------------
- // Constants
-
- #define kToolsPaletteWidth 41
- #define kToolsPaletteHeight 240
-
- const short kToolsPaletteRSRCID = 1007; // Resource id for the floating window with
- // tools palette
-
- //----------------------------------------------------------------------------------------
- // Setter for gShapesArray
- #pragma segment ShapeRes
-
- void SetShapeInArray(TShape* shape)
- {
- if (!gProtoShapes)
- {
- gProtoShapes = NewList();
- FailNIL(gProtoShapes);
- }
- gProtoShapes->InsertLast(shape);
- }
-
- //----------------------------------------------------------------------------------------
- // Accessor for gShapesArray
- #pragma segment ShapeRes
-
- TShape* GetShapeInArray(short shapeID)
- {
- #if qRangeCheck
- if ((shapeID < 0) || (gProtoShapes == NULL) || (shapeID >= gProtoShapes->fSize))
- {
- ProgramBreak("GetShapeInArray: shapeID out of range!");
- return NULL;
- }
- #endif
-
- return (TShape*) gProtoShapes->At(shapeID + 1);
- }
-
- //----------------------------------------------------------------------------------------
- // Accessor for gShapesArray
- #pragma segment ShapeRes
-
- short GetNumberOfShapes()
- {
- return (short) gProtoShapes->GetSize();
- }
-
- //========================================================================================
- // CLASS TToolsMenu
- //========================================================================================
- #undef Inherited
- #define Inherited TTearOffMenuView
-
- #pragma segment AInit
- MA_DEFINE_CLASS_M1(TToolsMenu, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TToolsMenu Constructor
- //----------------------------------------------------------------------------------------
- #pragma segment AInit
-
- TToolsMenu::TToolsMenu()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsMenu::IToolsMenu
- //----------------------------------------------------------------------------------------
- #pragma segment AInit
-
- void TToolsMenu::IToolsMenu(ResNumber menuID)
- {
- this->ITearOffMenuView(menuID, kToolsPaletteWidth, kToolsPaletteHeight, gToolsPaletteWindow);
-
- TToolsPalette* aToolsPalette = new TToolsPalette;
- aToolsPalette->IToolsPalette(NULL, this);
- aToolsPalette->fIdentifier = 'MTUL';
- }
-
- //========================================================================================
- // CLASS TToolsPalette
- //========================================================================================
- #undef Inherited
- #define Inherited TView
-
- #pragma segment AOpen
- MA_DEFINE_CLASS_M1(TToolsPalette, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette Constructor
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- TToolsPalette::TToolsPalette()
- {
- fCurrTool = 0;
- fOldTool = 0;
- fSelectedTool = 0;
-
- CRect r;
- short top = 0;
- for (short i=0; i <= kShapesInPalette; i++) // Define the palette choices
- {
- SetRect(r, 0, top, kToolsPaletteWidth - 1, top + kToolsPaletteWidth - 1);
- fChoiceArray[i] = r;
- top += kToolsPaletteWidth - 1;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::IToolsPalette
- //----------------------------------------------------------------------------------------
- #pragma segment AOpen
-
- void TToolsPalette::IToolsPalette(TDocument* itsDocument, TView* itsSuperView)
- {
- VPoint itsSize(kToolsPaletteWidth, kToolsPaletteHeight);
- this->IView(itsDocument, itsSuperView, gZeroVPt, itsSize, sizeFixed, sizeFixed);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::IToolsPalette
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TToolsPalette::DoMouseCommand(VPoint& theMouse,
- TToolboxEvent* /*event*/,
- CPoint /*hysteresis*/) // Override
- {
- TToolsPalette* menuToolsPalette = NULL;
- if (gToolsMenu)
- menuToolsPalette = (TToolsPalette*)gToolsMenu->FindSubView('MTUL');
-
- TToolsPalette* floatingToolsPalette = NULL;
- if (gToolsPaletteWindow)
- floatingToolsPalette = (TToolsPalette*)gToolsPaletteWindow->FindSubView('TPLT');
-
- TToolSelectCmd* aToolSelectCmd = new TToolSelectCmd;
- aToolSelectCmd->IToolSelectCmd(this, menuToolsPalette, floatingToolsPalette, theMouse);
-
- this->PostCommand(aToolSelectCmd);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::TrackFeedback
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TToolsPalette::TrackFeedback(TrackPhase aTrackPhase,
- const VPoint& /*anchorPoint*/,
- const VPoint& /*previousPoint*/,
- const VPoint& nextPoint,
- Boolean mouseDidMove,
- Boolean turnItOn) // Override
- {
- CPoint qdPt;
- CRect r;
-
- if (aTrackPhase == trackBegin)
- fSelectedTool = kNoToolSelection; // Start with no pre-selected tools
-
- if (mouseDidMove)
- {
- this->Focus();
- qdPt = this->ViewToQDPt(nextPoint);
- for (short i=0; i <= kShapesInPalette; i++)
- {
- r = fChoiceArray[i];
- if (PtInRect(qdPt, r) && (fSelectedTool != i))
- {
- this->FrameTool(r);
- if (turnItOn)
- {
- if (fSelectedTool != kNoToolSelection)
- {
- r = fChoiceArray[fSelectedTool];
- this->FrameTool(r); // Turn off the current tool
- }
- fSelectedTool = i; // remember the Tool currently selected
- }
- else
- fSelectedTool = kNoToolSelection; // no Tool currently selected
- break;
- }
- }
- }
-
- if (aTrackPhase == trackEnd)
- {
- if (fSelectedTool != kNoToolSelection)
- {
- r = fChoiceArray[fSelectedTool];
- this->FrameTool(r);
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::FrameTool
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TToolsPalette::FrameTool(CRect toolRect)
- {
- PenNormal();
- PenMode(patXor);
- PenSize(2, 2);
- FrameRect(toolRect);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::DoSetCursor
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TToolsPalette::DoSetCursor(const VPoint& /*localPoint*/, RgnHandle cursorRegion) // Override
- {
- CRect qdExtent;
-
- SetCursor(&(qd.arrow));
- this->GetQDExtent(qdExtent);
- RectRgn(cursorRegion, qdExtent);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::Draw
- //----------------------------------------------------------------------------------------
- #pragma segment ARes
-
- void TToolsPalette::Draw(const VRect& area) // Override
- {
- CRect r;
-
- PenSize(1, 1);
- MoveTo((short)fSize.h - 1, 0);
- Line(0, (short)fSize.v);
- for (short i=0; i <= kShapesInPalette; i++)
- {
- r = fChoiceArray[i];
- FrameRect(r);
- TShape* shape = GetShapeInArray(i);
- if (shape)
- shape->Draw();
- }
-
- if (fCurrTool != kNoToolSelection)
- this->Toggle(fCurrTool);
-
- Inherited::Draw(area);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::SelectNewTool
- //----------------------------------------------------------------------------------------
- #pragma segment ShapeRes
-
- void TToolsPalette::SelectNewTool(short whichTool)
- {
- if (this->IsShown())
- {
- this->Focus();
- this->Toggle(fCurrTool);
- fCurrTool = whichTool;
- this->Toggle(fCurrTool);
- }
- else
- fCurrTool = whichTool; // Just set the field so that it is correct when it is shown
- }
-
- //----------------------------------------------------------------------------------------
- // TToolsPalette::Toggle
- //----------------------------------------------------------------------------------------
- #pragma segment ShapeRes
-
- void TToolsPalette::Toggle(short theTool)
- {
- CRect r = fChoiceArray[theTool];
- UseSelectionColor();
- InvertRect(r);
- }
-
- //========================================================================================
- // CLASS TToolSelectCmd
- //========================================================================================
- #undef Inherited
- #define Inherited TTearOffMenuViewTracker
-
- #pragma segment AInit
- MA_DEFINE_CLASS_M1(TToolSelectCmd, Inherited);
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd Constructor
- //----------------------------------------------------------------------------------------
- #pragma segment AInit
-
- TToolSelectCmd::TToolSelectCmd()
- {
- fMenuToolsPalette = NULL;
- fFloatingToolsPalette = NULL;
- fTool = kNoToolSelection;
- fExitTracking = false;
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::IToolSelectCmd
- //----------------------------------------------------------------------------------------
- #pragma segment ASelCommand
-
- void TToolSelectCmd::IToolSelectCmd(TToolsPalette* theToolsPalette,
- TToolsPalette* theMenuToolsPalette,
- TToolsPalette* theFloatingToolsPalette,
- VPoint theMouse)
- {
- this->ITearOffMenuViewTracker(cChangeTool, NULL, kCantUndo, kDoesNotCauseChange,
- NULL, theToolsPalette, NULL, theMouse);
-
- fMenuToolsPalette = theMenuToolsPalette;
- fFloatingToolsPalette = theFloatingToolsPalette;
- fViewConstrain = false;
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::TrackMouse
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- TTracker* TToolSelectCmd::TrackMouse(TrackPhase aTrackPhase,
- VPoint& /*anchorPoint*/,
- VPoint& /*previousPoint*/,
- VPoint& nextPoint,
- Boolean /*mouseDidMove*/) // Override
- {
- TTracker* newTracker = this;
- if (gTrackingInMenu)
- {
- // quit tracking when the mouse leaves the view
- // - if gTrackingInMenu is TRUE, view constrain is set to FALSE, so nextPoint CAN leave the view
- // - all other times, view constrain is TRUE, so nextPoint WON'T leave the view
-
- if (fView && fView->IsShown())
- fExitTracking = !fView->ContainsMouse(nextPoint);
-
- // if gTrackingInMenu is TRUE, then fExitTracking might get set to TRUE above, meaning
- // that the user is either returning to the menubar or trying to tear off this menu, so
- // simply return NULL when we're done tracking and the tracker will be freed by
- // TApplication::TrackMouse
-
- if (aTrackPhase == trackRelease && fExitTracking)
- newTracker = NULL;
- }
-
- return newTracker;
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::TrackFeedback
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TToolSelectCmd::TrackFeedback(TrackPhase aTrackPhase,
- const VPoint& anchorPoint,
- const VPoint& previousPoint,
- const VPoint& nextPoint,
- Boolean mouseDidMove,
- Boolean turnItOn) // Override
- {
- // All we really want to do here is let the view give us some feedback, so...
- if (fView && fView->IsShown() && mouseDidMove)
- fView->TrackFeedback(aTrackPhase, anchorPoint, previousPoint, nextPoint, mouseDidMove, turnItOn);
-
- // When we're selecting a tool (i.e. turning it on, then this command needs to know
- // about the new selection. When we're deselecting a tool (i.e. turning it off) then this
- // command MUST ignore the deselection since MacApp's tracking will turn off the current
- // selection as part of its default tracking behavior even when the user has made a valid
- // selection in the view.
- if (turnItOn)
- fTool = ((TToolsPalette*)fView)->GetSelectedTool();
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::IsDoneTracking
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- Boolean TToolSelectCmd::IsDoneTracking() // Override
- {
- return (!StillDown() || fExitTracking);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::DoIt
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TToolSelectCmd::DoIt() // Override
- {
- // Tell the TToolsPalette in the menu which tool is selected
- if (fMenuToolsPalette)
- {
- fMenuToolsPalette->fOldTool = fMenuToolsPalette->fCurrTool;
- fMenuToolsPalette->fCurrTool = fTool;
- }
-
- // Tell the TToolsPalette in the floating window which tool is selected
- if (fFloatingToolsPalette)
- {
- fFloatingToolsPalette->fOldTool = fFloatingToolsPalette->fCurrTool;
- fFloatingToolsPalette->SelectNewTool(fTool);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::RedoIt
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TToolSelectCmd::RedoIt() // Override
- {
- // Tell the TToolsPalette in the menu which tool is selected
- if (fMenuToolsPalette)
- fMenuToolsPalette->fCurrTool = fTool;
-
- // Tell the TToolsPalette in the floating window which tool is selected
- if (fFloatingToolsPalette)
- fFloatingToolsPalette->SelectNewTool(fTool);
- }
-
- //----------------------------------------------------------------------------------------
- // TToolSelectCmd::UndoIt
- //----------------------------------------------------------------------------------------
- #pragma segment ADoCommand
-
- void TToolSelectCmd::UndoIt() // Override
- {
- // Tell the TToolsPalette in the menu which tool *was* selected
- if (fMenuToolsPalette)
- fMenuToolsPalette->fCurrTool = fMenuToolsPalette->fOldTool;
-
- // Tell the TToolsPalette in the floating window which tool *was* selected
- if (fFloatingToolsPalette)
- fFloatingToolsPalette->SelectNewTool(fFloatingToolsPalette->fOldTool);
- }
-
- //========================================================================================
- // GLOBAL Functions
- //========================================================================================
- #undef Inherited
-
- //----------------------------------------------------------------------------------------
- // GetToolsPalette
- //----------------------------------------------------------------------------------------
- #pragma segment ShapeRes
-
- TToolsPalette* GetToolsPalette()
- {
- return (TToolsPalette*) gToolsMenu->FindSubView('MTUL');
- }
-
-